package com.shushan.setting;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.home.TabHomeActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.SharedPreferencesUtils;

/**
 * @ClassName: ParentPswSetActivity
 * @Desciption: 家长密码设置界面
 * @author: yichaohua
 * @date: 2018-07-13
 */
public class ParentPswSetActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "ParentPswSetActivity";

    private Button confirmButton;
    private EditText passwdEdit;
    private ImageView backImg;

    private boolean isParentSet;
    private int firstTime = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.parent_setting_activity);

        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true,this);

        isParentSet = getIntent().getBooleanExtra("isParentSet",false);
        firstTime = getIntent().getIntExtra("firstTime",0);

        initView();
    }

    private void initView() {
        confirmButton = findViewById(R.id.parent_setting_confrim_btn);
        confirmButton.setOnClickListener(this);
        passwdEdit = findViewById(R.id.parent_setting_passwd_edit);
        backImg = findViewById(R.id.parent_setting_back_img);
        backImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.parent_setting_confrim_btn:
                if(valide()){
                    setParentPasswd();
                }
                break;
            case R.id.parent_setting_back_img:
                if(isParentSet){
                    onBackAction();
                }else {
                    showBackDialog();
                }
                break;
            default:
                break;
        }
    }

    private boolean valide() {
        String passwd = passwdEdit.getText().toString();
        if(TextUtils.isEmpty(passwd) || (passwd.length() != 6)){
            showToast("请输入6位家长密码");
            return false;
        }

        return true;
    }

    private void setParentPasswd() {
        showWaitDialog();

        int useId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.PARENT_PASSWD_SET);
        urlParse.putValue("user_id", useId)
                .putValue("password", passwdEdit.getText().toString());

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        Log.i(TAG, "onSuccess data=" + data);

                        SharedPreferencesUtils.setStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY,passwdEdit.getText().toString());

                        if(!isParentSet){
                            SharedPreferencesUtils.setBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY,true);
                            Intent intent = new Intent(ParentPswSetActivity.this,TabHomeActivity.class);
                            intent.putExtra("firstTime",1);
                            startActivityAllow(intent);
                        }

                        onBackAction();
                        showToast("家长密码设置成功");
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private void showBackDialog() {
        new CustomDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("发现您尚未完成家长密码设置，退出将导致无法管控您的小孩，是否退出设置？")
                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        onBackAction();
                    }
                })
                .setPositiveButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                .create().show();
    }

    @Override
    public void onBackPressed() {
        if(isParentSet){
            onBackAction();
        }else {
            showBackDialog();
        }
    }
}
